package cn.sx.service;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 后台管理员service
 *@Time: 2020/9/20  23:21
 */

import cn.sx.dao.AdminDao;
import cn.sx.entity.Admins;
import cn.sx.entity.Type;
import cn.sx.entity.User;
import cn.sx.util.SafeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private TypesService typesService;

    /*根据用户名和密码查询（管理员登录）*/
    public Admins getByUsernameAndPassword(String username, String password){
        return adminDao.selectByAdminnameAndPassword(username, SafeUtil.encode(password));
    }

    /*修改后台管理员密码*/
    public boolean updatePassword(int id,String  password){
        return adminDao.updatePassword(id,SafeUtil.encode(password));
    }




    /*显示管理员*/
    public List<Admins> findAllAdmins(){
        return adminDao.findAll();
    }

    /*根据用户名查询管理员*/
    public Admins selectAdminByUserame(String username) {
        return adminDao.selectAdminByUsername(username);
    }
    /*添加管理员*/
    public boolean addAdmin(Admins admins){
        /*密码加密*/
        admins.setPassword(SafeUtil.encode(admins.getPassword()));
        return adminDao.addAdmin(admins);
    }
    /*删除管理员*/
    public int deleteAdmin(int id){
        return adminDao.deleteAdmins(id);
    }
    /*修改后台用户信息*/
    public boolean updateUser(int id,String name,String phone,String address){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);
        return adminDao.updateUser(user);
    }

    public Admins get(int id) {
        return adminDao.select(id);
    }

    public long getCount() {
        return adminDao.selectCount();
    }
}
